package com.wenhai.concurrency.atomic;

import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.LongAdder;

/**
 *
 * {@link AtomicLong} 作为累加器示例
 * @ClassName atomicLongDemo
 * @Description
 * @Author 谢文海
 * @Date 2021/4/13 1:12
 * @Version 1.0
 * @see AtomicLong
 * @see LongAdder
 **/
public class AtomicLongDemo {
    private static AtomicLong atomicLong = new AtomicLong();
    private static Integer[] arrayOne = {0, 1, 2, 3, 0, 5, 0, 56, 0};
    private static Integer[] arrayTwo = {10, 1, 2, 3, 0, 5, 0, 56, 0};

    public static void main(String[] args)throws Exception {
        Thread threadA = new Thread(() -> {
            for (int i = 0; i < arrayOne.length; i++) {
                if (arrayOne[i] == 0) {
                    atomicLong.incrementAndGet();
                }
            }
        });
        Thread threadB = new Thread(() -> {
            for (int i = 0; i < arrayTwo.length; i++) {
                if (arrayTwo[i] == 0) {
                    atomicLong.incrementAndGet();
                }
            }
        });
        threadA.start();
        threadB.start();
        threadA.join();
        threadB.join();
        System.out.println("count 0: " + atomicLong.get());
    }
}
